package co.salehi.domain;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import javax.validation.constraints.NotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.springframework.format.annotation.DateTimeFormat;
import co.salehi.domain.BatteryMake;
import javax.persistence.ManyToOne;

@RooJavaBean
@RooToString
@RooEntity(finders = { "findBatterysByAmper" })
public class Battery {
	
	@NotNull
	private Integer count;

	@NotNull
	private String amper;

	@NotNull
	@ManyToOne
	private BatteryMake brand;

	@NotNull
	private Integer buyPrice;

	private Integer sellPrice;

	private Integer receiptPrice;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd-MM-yyyy")
	private Date insertDate = new Date();

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd-MM-yyyy")
	private Date sellDate;

	public String toString() {
		String str = amper + " " + brand.getName() + " " + brand.getCountry();
		if (sellPrice != null)
			str += ":" + sellPrice;
		return str;
	}

	public boolean equals(Object obj) {
		if (obj instanceof Battery)
			return getId().equals(((Battery) obj).getId());
		else
			return false;
	}

	public static List<Battery> findAllBatterys() {
		if (Country.countCountrys() == 0
				|| BatteryMake.countBatteryMakes() == 0)
			return new ArrayList<Battery>();

		return entityManager().createQuery(
				"select o from Battery o where o.sellDate is null",
				Battery.class).getResultList();
	}

	public static List<Battery> findBatteryEntries(int firstResult,
			int maxResults) {
		if (Country.countCountrys() == 0
				|| BatteryMake.countBatteryMakes() == 0)
			return new ArrayList<Battery>();

		return entityManager()
				.createQuery(
						"select o from Battery o where o.sellDate is null",
						Battery.class).setFirstResult(firstResult)
				.setMaxResults(maxResults).getResultList();
	}

	public static long countBatterys() {
		return entityManager().createQuery(
				"select count(o) from Battery o where o.sellDate is null ",
				Long.class).getSingleResult();
	}

	public static TypedQuery<Battery> findBatterysByAmper(String amper) {
		if (amper == null || amper.length() == 0)
			throw new IllegalArgumentException("Amper is required");
		EntityManager em = Battery.entityManager();
		TypedQuery<Battery> q = em
				.createQuery(
						"SELECT Battery FROM Battery AS battery WHERE LOWER(battery.amper) LIKE LOWER(:amper) and battery.sellDate is null",
						Battery.class);
		q.setParameter("amper", amper);
		return q;
	}

	public static Object findSoldBatteryEntries(int firstResult, int maxResults) {
		if (Country.countCountrys() == 0
				|| BatteryMake.countBatteryMakes() == 0)
			return new ArrayList<Battery>();
		return entityManager()
				.createQuery(
						"select o from Battery o where o.sellDate is not null",
						Battery.class).setFirstResult(firstResult)
				.setMaxResults(maxResults).getResultList();
	}

	public static float countSoldBatteries() {
		return entityManager().createQuery(
				"select count(o) from Battery o where o.sellDate is not null ",
				Long.class).getSingleResult();
	}

	public static Object findAllSoldBatteries() {
		if (Country.countCountrys() == 0
				|| BatteryMake.countBatteryMakes() == 0)
			return new ArrayList<Battery>();

		return entityManager().createQuery(
				"select o from Battery o where o.sellDate is not null",
				Battery.class).getResultList();

	}
}
